package com.hcb.tonguetiphealth.widget.resbook;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.hcb.tonguetiphealth.adapter.GeneralPageAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.JSONManager;
import com.hcb.tonguetiphealth.model.Drinks;
import com.hcb.tonguetiphealth.model.HotResPackage;
import com.hcb.tonguetiphealth.model.Restaurant;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.view.LoginActivity;
import com.hcb.tonguetiphealth.view.MoreActivity;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.view.ResBookActivity;
import com.hcb.tonguetiphealth.view.SearchActivity;
import com.hcb.tonguetiphealth.widget.GeneralWidget;
import com.hcb.tonguetiphealth.widget.home.HomeWidget;

public class ResearchWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "ResearchWidget";
	
	private Activity ctx;
	private Handler mHandler;
	private Button btSearch, btBack;
	private LinearLayout llBack;
	private EditText etSearch;
	private LayoutInflater inflater;
	private ImageView iv, iv1, iv2, iv3, iv4, iv5, iv6;
	private int crrentPageId = 0;
//	private LinearLayout llCity, llSearch, llHotRes, llLogin, llMore ;
	private List <HotResPackage> hotResPackageList = new ArrayList<HotResPackage>();
	
	private ViewPager viewPager;
	private List<View> pageViews;
	private View view;
	private GeneralPageAdapter pageAdapter;
	
	private final static int SUCCESS = 0;
	
	public ResearchWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.quick_search_widget, this); 
		
		inflater = ctx.getLayoutInflater();
		etSearch = (EditText)findViewById(R.id.et_quick_search);
		btSearch = (Button)findViewById(R.id.bt_quick_search);
		llBack = (LinearLayout)findViewById(R.id.search_linearlayout_back);
		viewPager = (ViewPager)findViewById(R.id.hot_res_viewpager);
		
//		llCity = (LinearLayout)findViewById(R.id.linearlayout_home_city);
//		llSearch = (LinearLayout)findViewById(R.id.linearlayout_home_search);
//		llHotRes = (LinearLayout)findViewById(R.id.linearlayout_home_res);
//		llLogin = (LinearLayout)findViewById(R.id.linearlayout_home_login);
//		llMore = (LinearLayout)findViewById(R.id.linearlayout_home_more);
//		
//		llCity.setOnClickListener(this);
//		llSearch.setOnClickListener(this);
//		llHotRes.setOnClickListener(this);
//		llLogin.setOnClickListener(this);
//		llMore.setOnClickListener(this);
		
		btSearch.setOnClickListener(this);
		llBack.setOnClickListener(this);
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
					case SUCCESS:
						initDataPageView();
						break;
					}
				}
			};
			
		new Thread(){
			public void run() {
				getData();
		//		reSort();
				Message message = new Message();
				message.what = SUCCESS;
				mHandler.sendMessage(message);
			}
		}.start();
		
	}
	public void onClick(View v) {
		Intent intent = new Intent();
		switch(v.getId()) {
		case R.id.bt_quick_search:
			String resName = etSearch.getText().toString();
			if(resName !=null && !resName.equals("") ){
				intent.putExtra("resName", resName);
				intent.setClass(ctx, ResBookActivity.class); 
				ctx.startActivity(intent);
			}else {
				Toast.makeText(ctx, "搜索关键字不能为空", Toast.LENGTH_LONG).show();
			}
			break;
//		case R.id.linearlayout_home_city:
//			break;
//		case R.id.linearlayout_home_search:
//			intent.setClass(ctx, SearchActivity.class);
//			ctx.startActivity(intent);
//			ctx.finish();
//			break;
//		case R.id.linearlayout_home_res:
//			break;
//		case R.id.linearlayout_home_login:
//			intent.setClass(ctx, LoginActivity.class);
//			ctx.startActivity(intent);
//			ctx.finish();
//			break;
//		case R.id.linearlayout_home_more:
//			intent.setClass(ctx, MoreActivity.class);
//			ctx.startActivity(intent);
//			ctx.finish();
//			break;
		case R.id.search_linearlayout_back:
//			ctx.setContentView(new HomeWidget(ctx));
			ctx.finish();
			break;
		default:
			toDetail(v.getId());
			break;
		}
		 
	}
	private void toDetail(int id) {
		int num = 0;
		switch(id) {
		case R.id.hot_res_pageview_img1:
			num += 0;
			break;
		case R.id.hot_res_pageview_img2:
			num += 1;
			break;
		case R.id.hot_res_pageview_img3:
			num += 2;
			break;
		case R.id.hot_res_pageview_img4:
			num += 3;
			break;
		case R.id.hot_res_pageview_img5:
			num += 4;
			break;
		case R.id.hot_res_pageview_img6:
			num += 5;
			break;
		}
		int position = crrentPageId*6 + num;
		HotResPackage hotResPackage = hotResPackageList.get(position);
		if (hotResPackage.getType() == 0) {//商家
			Intent intent = new Intent();
			intent.setClass(ctx, ResBookActivity.class);
			intent.putExtra("Search", "true");
			intent.putExtra("resId", hotResPackage.getId());
			ctx.startActivity(intent);
		}else {//套餐
			
		}
	 
	}
 
	
	/**
	 * 从服务端获取热门商家、套餐数据
	 */
	private void getData(){
		
		List <NameValuePair> params = new ArrayList <NameValuePair>();
		params.add(new BasicNameValuePair("city", "129"));
		
		String response = HttpUtils.Post(Global.getServletHotRes(), params);
		System.out.println("热门商家套餐："+response);
		hotResPackageList = new ArrayList<HotResPackage>();
		JSONManager jsonManager = new JSONManager();
		if(jsonManager.isGotJSONObject(response)){
			JSONArray jsonArray = jsonManager.getJsonList();
			for (int i = 0; i < jsonArray.length() && i< jsonManager.getResult() && jsonArray.length() == 20 ; i++) {
				try {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					HotResPackage obj = new HotResPackage();
					if (i < 10 && i<6) {
						//热门套餐
						obj.setId(jsonObject.getString("itemid"));
						obj.setImgURL(jsonObject.getString("thumb"));
						obj.setType(1);
						hotResPackageList.add(obj);
						
					}else if (i >= 10 && i<16) {
						//热门商家
						obj.setId(jsonObject.getString("userid"));
						obj.setImgURL(jsonObject.getString("thumb"));
						obj.setType(0);
						hotResPackageList.add(obj);
					}
					
				
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			List <HotResPackage> tempList = new ArrayList<HotResPackage>();
			 
			for(int i=6; i < hotResPackageList.size(); i++){
				tempList.add(hotResPackageList.get(i));
			}
			for (int i = 0; i < 6; i++) {
				tempList.add(hotResPackageList.get(i));
			}
			hotResPackageList = tempList;
		}
	}
	
	private void initDataPageView() {
		 
		pageViews = new ArrayList<View>();
		System.out.println("hotResPackageList.size():"+hotResPackageList.size());
		int pageCount = hotResPackageList.size()/6 + (hotResPackageList.size()%6 == 0?0:1);
		System.out.println("hotResPackageList.size():pageCount；"+pageCount);
		for(int i=0; i< pageCount; i++) {
			pageViews.add(inflater.inflate(R.layout.hot_res_pageview, null));
		}
		
		HotResPackage hotResPackage;
		
		for(int i=0; i<pageViews.size(); i++) {
			view = pageViews.get(i);
			iv1 = (ImageView)view.findViewById(R.id.hot_res_pageview_img1);
			iv2 = (ImageView)view.findViewById(R.id.hot_res_pageview_img2);
			iv3 = (ImageView)view.findViewById(R.id.hot_res_pageview_img3);
			iv4 = (ImageView)view.findViewById(R.id.hot_res_pageview_img4);
			iv5 = (ImageView)view.findViewById(R.id.hot_res_pageview_img5);
			iv6 = (ImageView)view.findViewById(R.id.hot_res_pageview_img6);
			int count = hotResPackageList.size() - (i+1)*6;
			if(count >= 0) {
				for(int j=1; j<=6; j++) {
					hotResPackage = hotResPackageList.get(i*6 + j-1);
					initImage(j, hotResPackage);
				}
			} else {
				count = hotResPackageList.size() - i*6;
				for(int j=1; j<=count; j++) {
					hotResPackage = hotResPackageList.get(i*6 + j-1);
					initImage(j, hotResPackage);
				}
				switch(count) {
				case 1:
					iv2.setVisibility(ImageView.INVISIBLE);
					iv3.setVisibility(ImageView.INVISIBLE);
					iv4.setVisibility(ImageView.INVISIBLE);
					iv5.setVisibility(ImageView.INVISIBLE);
					iv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 2:
					iv3.setVisibility(ImageView.INVISIBLE);
					iv4.setVisibility(ImageView.INVISIBLE);
					iv5.setVisibility(ImageView.INVISIBLE);
					iv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 3:
					iv4.setVisibility(ImageView.INVISIBLE);
					iv5.setVisibility(ImageView.INVISIBLE);
					iv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 4:
					iv5.setVisibility(ImageView.INVISIBLE);
					iv6.setVisibility(ImageView.INVISIBLE);
					break;
				case 5:
					iv6.setVisibility(ImageView.INVISIBLE);
					break;
				}
			}
			
		}
		
		pageAdapter = new GeneralPageAdapter(pageViews);
		viewPager.setAdapter(pageAdapter);
		viewPager.setOnPageChangeListener(new GuidePageChangeListener());
	}
	private void initImage(int id, HotResPackage hotResPackage) {
		switch(id) {
		case 1:
			loadImage(iv1, hotResPackage.getImgURL());
			iv1.setOnClickListener(this);
			break;
		case 2:
			loadImage(iv2, hotResPackage.getImgURL());
			iv2.setOnClickListener(this);
			break;
		case 3:
			loadImage(iv3, hotResPackage.getImgURL());
			iv3.setOnClickListener(this);
			break;
		case 4:
			loadImage(iv4, hotResPackage.getImgURL());
			iv4.setOnClickListener(this);
			break;
		case 5:
			loadImage(iv5, hotResPackage.getImgURL());
			iv5.setOnClickListener(this);
			break;
		case 6:
			loadImage(iv6, hotResPackage.getImgURL());
			iv6.setOnClickListener(this);
			break;
		}
	}
	// 指引页面更改事件监听器
	class GuidePageChangeListener implements OnPageChangeListener {

		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageSelected(int arg0) {
			crrentPageId = arg0;
			switch(arg0) {
			case 0:
				break;
			case 1:
				break;
			case 2:
				break;
			}
		}
	}
	
 
	
	@Override
	public void notifyAdapter() {
		
	}
}
